<?php

namespace App\Admin\Controllers;

use App\Models\Area;
use Cblink\Region\Region;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AreaController extends AdminController
{
    public function city(Request $request)
    {
        $parent_id = $request->get('q');
        // 直辖区市
        $arr = [
            '天津市',
            '北京市',
            '重庆市',
            '上海市',
            '香港特别行政区',
            '澳门特别行政区',
        ];

        if (in_array(Area::query()->where('id', $parent_id)->first()->name, $arr)) {
            return collect([['id' => 1, 'text' => " - "]]);
        }

        return Area::query()->where('parent_id', $parent_id)->get(['id', DB::raw('name as text')]);
    }

    public function area(Request $request)
    {
        $parent_id = $request->get('q');

        return Area::query()->where('parent_id', $parent_id)->get(['id', DB::raw('name as text')]);
    }
}
